package com.webrtc

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zh.kotlin_mvvm.R
import com.zh.kotlin_mvvm.databinding.OnlineUsrsItemBinding

class OnlineUserAdapter(
    private val data: List<OnlineUser>,
    private val onCall: (id: String) -> Unit
) : RecyclerView.Adapter<OnlineUserAdapter.OuViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OuViewHolder {
        val view = OnlineUsrsItemBinding.bind(
            LayoutInflater.from(parent.context).inflate(R.layout.online_usrs_item, parent, false)
        )
        return OuViewHolder(view)
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: OuViewHolder, position: Int) {
        holder.onBind(data[position], onCall)
    }

    class OuViewHolder(private val binding: OnlineUsrsItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun onBind(
            user: OnlineUser,
            onCall: (id: String) -> Unit
        ) {
            binding.userNameTextView.text = user.name
            binding.userIdTextView.text = user.id
            binding.callButton.setOnClickListener { onCall.invoke(user.id) }
        }
    }
}
